<?php
/**
 * Notes:
 * User: Administrator
 * Date: 2020/3/6
 * Time: 23:13
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;
use think\Log;
use app\api\model\Category as CategoryModel;

class Category extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 技能default分类查找
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $category = Db::name('category')->where('type', 'default')->field('id,pid,name')->cache(600)->select();
        $date = listToTree($category);

        $this->success('', $date);

    }

    /**
     * 岗位列表
     */
    public function stations()
    {
        $list = CategoryModel::findListByCondition(['pid' => 0, 'type' => 'default']);
        $listData = [];
        if ($list) {
            foreach ($list as $listVal) {
                //获取子分类
                $childs = CategoryModel::findListByCondition(['pid' => $listVal['id']]);
                $childsData = [];
                if ($childs) {
                    foreach ($childs as $childVal) {
                        $childsData[] = [
                            'id' => $childVal['id'],
                            'name' => $childVal['name']
                        ];
                    }
                }
                unset($childs);
                $listData[] = [
                    'id' => $listVal['id'],
                    'name' => $listVal['name'],
                    'childs' => $childsData ? $childsData : []
                ];
                unset($childsData);
            }
        }
        $res = [
            'list' => $listData
        ];
        $this->success('success', $res);
    }
}